using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using ToDoListWeb.Models;
using ToDoListWeb.ViewModels;

namespace ToDoListWeb.Service
{
    public class UserService
    {
        private ToDoListWebContext _ctx = new ToDoListWebContext();

        /// <summary>
        /// 用户注册；
        /// 若用户名已存在，抛出异常；
        /// 若用户名不存在，新建Users对象，添加到_ctx下Users列表中；
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPassword"></param>
        public bool UserRegister(string userName, string userPassword,string nickName)                          //用户注册                                                                          //用户注册：使用三个参数：用户昵称、用户密码、用户名。
        {
            if (_ctx.Users.SingleOrDefault(p => p.UserName == userName) == null)
            {
                UsersService user = new UsersService()
                {
                    UserName = userName,
                    Password = userPassword,
                    NickName=nickName,
                    RegistTime=DateTime.Now
                };
                _ctx.Users.Add(user);
                _ctx.SaveChanges();
                return true;
            }
            return false;
        }
        /// <summary>
        /// 用户登录；
        /// 若所给数据在_ctx下的Users列表中无匹配项，抛出异常；
        /// 若有匹配项，返回该所匹配对象的userId；
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPassword"></param>
        /// <returns></returns>
        public int UserLogin(string userName, string userPassword)                       
        {
            var user = _ctx.Users.SingleOrDefault(props => props.UserName == userName&&props.Password==userPassword);
            if(user == null)
                 return 0;
            return user.UserId;
        }
        ///// <summary>
        ///// 获取用户列表信息;
        ///// 对用户对象中的列表初始化；
        ///// </summary>
        ///// <param name="userId"></param>
        //public void GetUserList(int userId)
        //{
        //    UsersService user = GetUser(userId);
        //    user.Inventory = _ctx.Inventory.Where(p => p.UserId == user.UserId).ToList();
        //    user.Matter = _ctx.Matter.Where(p => p.UserId == user.UserId).ToList();
        //}
        /// <summary>
        /// 通过userId获取一个Users对象；
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public UsersService GetUser(int userId)
        {
            return _ctx.Users.Include("Inventory").Include("Type").Include("Matter").SingleOrDefault(p => p.UserId == userId);
        }
        /// <summary>
        /// 以UserModel的形式返回部分User对象
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public UserModel GetUserModel(int userId)
        {
            UsersService user = GetUser(userId);
            return new UserModel
            {
                UserId=userId,
                UserName=user.UserName,
                NickName=user.NickName,
                RegistTime=user.RegistTime,
                Type=user.Type.ToList(),
                Inventory=user.Inventory.ToList(),
                Matter=user.Matter.ToList()
            };
        }
        /// <summary>
        /// 修改用户昵称
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="nickName"></param>
        public void ChangeNickName(int userId,string nickName)
        {
            _ctx.Users.SingleOrDefault(u=>u.UserId==userId).NickName=nickName;
            _ctx.SaveChanges();
        }
        /// <summary>
        /// 从数据库更新UserModel内容
        /// </summary>
        /// <param name="userModel"></param>
        public void UpdateUserModel(UserModel userModel)
        {
            var user = _ctx.Users.Include("Type").Include("Inventory").Include("Matter").SingleOrDefault(u=>u.UserId==userModel.UserId);
            userModel.NickName = user.NickName;
            userModel.Type = user.Type;
            userModel.Inventory = user.Inventory;
            userModel.Matter = user.Matter;
            IsPastdueTime(userModel.UserId);
        }

        public MenuInfoModel GetMenuInfoModel(UserModel user)
        {
            MenuInfoModel menuModel = new MenuInfoModel()
            {
                User=user,
                NumberOfMatter=user.Matter.Count,
                NumberOfPastdueMatter=GetPastdueMatter(user.UserId).Count
            };
            return menuModel;
        }
        /// <summary>
        /// 对该userId对应所有Matter进行判断；
        /// 如果对应事件未设置过期时间，事件未过期，IsPastdue设置为false；
        /// 如果对应事件已过期，IsPastdue设置为true;
        /// 如果对应时间未过期，IsPastdue设置为false;
        /// </summary>
        /// <param name="userId"></param>
        public void IsPastdueTime(int userId)                                                   //计算事件是否过期
        {
            foreach (var item in _ctx.Matter.Where(p => p.UserId == userId).ToList())
            {
                if (item.OverTime != null && item.OverTime < DateTime.Now)
                    item.IsPastdue = true;
                else
                    item.IsPastdue = false;
            }
            _ctx.SaveChanges();
        }
        /// <summary>
        /// 先对userId中所有Matter进行判断；
        /// 返回Matter中所有IsPastdue值为true的事件；
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<Matter> GetPastdueMatter(int userId)                                        //统计过期事件
        {
            IsPastdueTime(userId);
            List<Matter> loseValueMatter = _ctx.Matter.Where(props => props.UserId == userId && props.IsPastdue == true).ToList();
            return loseValueMatter;
        }
    }
}
